const { ethers } = require('ethers');

// BSC Testnet配置
const BSC_TESTNET_RPC = 'https://data-seed-prebsc-1-s1.binance.org:8545/';
const CONTRACT_ADDRESS = '0x031b8B821B0F1A87D1a1451479d29660Bc68d2C8';

// 合约ABI
const contractABI = [
  "function tokenLaunches(uint256) external view returns (address tokenAddress, address creator, string name, string symbol, string description, string website, string telegram, string twitter, uint256 targetAmount, uint256 raisedAmount, uint256 tokensSold, uint256 createdAt, uint256 deadline, uint256 bondingCurveK, uint256 totalSupplyForSale, bool launched, bool cancelled)",
  "event TokenLaunchCreated(uint256 indexed launchId, address indexed creator, address indexed tokenAddress, string name, string symbol, uint256 targetAmount)",
  "event TokenGraduated(uint256 indexed launchId, address indexed tokenAddress, address indexed pair, uint256 liquidityBNB, uint256 liquidityTokens)",
  "event LaunchCancelled(uint256 indexed launchId, address indexed creator)"
];

async function checkLaunchHistory() {
  console.log('🔍 检查Launch历史事件...\n');
  
  try {
    const provider = new ethers.JsonRpcProvider(BSC_TESTNET_RPC);
    const contract = new ethers.Contract(CONTRACT_ADDRESS, contractABI, provider);
    
    const launchId = 2;
    
    // 获取创建事件
    const createFilter = contract.filters.TokenLaunchCreated(launchId);
    const createEvents = await contract.queryFilter(createFilter, -1000);
    
    console.log('创建事件:', createEvents.length > 0 ? createEvents[0].args : '未找到');
    
    // 获取毕业事件
    const graduateFilter = contract.filters.TokenGraduated(launchId);
    const graduateEvents = await contract.queryFilter(graduateFilter, -1000);
    
    console.log('毕业事件:', graduateEvents.length > 0 ? graduateEvents[0].args : '未找到');
    
    // 获取取消事件
    const cancelFilter = contract.filters.LaunchCancelled(launchId);
    const cancelEvents = await contract.queryFilter(cancelFilter, -1000);
    
    console.log('取消事件:', cancelEvents.length > 0 ? cancelEvents[0].args : '未找到');
    
    // 获取launch详细信息
    const launch = await contract.tokenLaunches(launchId);
    console.log('\n当前Launch状态:');
    console.log('- Launched:', launch.launched);
    console.log('- Cancelled:', launch.cancelled);
    console.log('- Target Amount:', ethers.formatEther(launch.targetAmount), 'BNB');
    console.log('- Raised Amount:', ethers.formatEther(launch.raisedAmount), 'BNB');
    console.log('- 达成比例:', ((Number(ethers.formatEther(launch.raisedAmount)) / Number(ethers.formatEther(launch.targetAmount))) * 100).toFixed(2), '%');
    
  } catch (error) {
    console.error('❌ 检查失败:', error.message);
  }
}

checkLaunchHistory();
